package cn.ccenjie.wsy.social;

import cn.ccenjie.wsy.config.jwt.Jwtconst;
import cn.ccenjie.wsy.vo.Resp;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;

@RestController
@RequestMapping("/social")
public class SocialUrlHandlerController {


    @RequestMapping("/success")
    public Resp doSuccess(HttpServletResponse response) {

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

        String token = Jwts.builder()
                .setSubject(authentication.getName())
                .claim(Jwtconst.USER_KEY, authentication)
                .claim(Jwtconst.AUTHORITYS_KEY, authentication.getAuthorities())
                .setExpiration(new Date(System.currentTimeMillis() + 60 * 1000))
                .signWith(SignatureAlgorithm.HS512, Jwtconst.SIG_KEY)
                .compact();

        return Resp.ok().data(token);
    }
}
